/**
 * 自定义用户
 * */

var formList = [];
var cateArys = [];


//选项
function getOption(){
    $.ajax({
        type: apis.caseDesignerOption.method,
        url: apis.caseDesignerOption.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            cateArys = res.data.cate;
            var h = '', i;
            for (i = 0; i < cateArys.length; i ++) {
                h += '<option value="' + cateArys[i].id + '">';
                h += cateArys[i].title;
                h += '</option>';
            }
            $('#cateid').html(h);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//公司列表
function getIndex(){
    var formData = {
        start: pages.start,
        limit: pages.limit
    };
    $.ajax({
        type: apis.caseDesignerIndex.method,
        url: apis.caseDesignerIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            $('#pageTotal').html(res.total);
            comHtml(d);
            var pageObj = pages; pageObj.total = res.total;
            pageHtml(pageObj);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}
//公司字符串
function comHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].realname + '</td>';
        h += '<td>';
        if (d[i].head == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].head + '" height="50"/>';
        }
        h += '</td>';
        h += '<td>' + d[i].work_year + '</td>';
        h += '<td>' + d[i].cate_name + '</td>';
        h += '<td>' + d[i].score + '</td>';
        h += '<td>' + d[i].cname + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="lay_edit(\'编辑\',\'designer-edit.html\', ' + d[i].id + ')" style="text-decoration:none">';
        h += '修改';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="lay_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $.ajax({
        type: apis.caseDesignerOne.method,
        url: apis.caseDesignerOne.url,
        data: { id : id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i;
            $('#realname')[0].value = d.realname;
            if (d.head != '') {
                h = '';
                h += '<img src="/' + d.head + '"/>';
                h += '<input type="hidden" id="img" value="' + d.head + '"/>';
                $('#bigShow').html(h);
            }
            if (d.cate_id > 0) {
                for (i = 0; i < cateArys.length; i ++) {
                    if (d.cate_id == cateArys[i].id) {
                        h += '<option value="' + cateArys[i].id + '" selected>';
                    } else {
                        h += '<option value="' + cateArys[i].id + '">';
                    }
                    h += cateArys[i].title;
                    h += '</option>';
                }
                $('#cateid').html(h);
            }
            $('#work_year')[0].value = d.work_year;
            $('#cname')[0].value = d.cname;
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//显示图片
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).height(e.h);
}


//保存
function modify(){
    var formData = {
        id: id,
        realname: $('#realname').val(),
        img: $('#img').val(),
        cateid: $('#cateid').val(),
        work_year: $('#work_year').val(),
        cname: $('#cname').val(),
    };
    if (!formData.realname) {
        msgPop({msg: '姓名必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.caseDesignerModify.method,
        url: apis.caseDesignerModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//删除
function lay_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.caseDesignerDel.method,
            url: apis.caseDesignerDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}